package com.mao.jxc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mao.jxc.admin.pojo.User;
import com.mao.jxc.admin.mapper.UserMapper;
import com.mao.jxc.admin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mao.jxc.admin.utils.AssertUtil;
import com.mao.jxc.admin.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author mao
 * @since 2021-07-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String userName, String password) {

        AssertUtil.isTrue(StringUtil.isEmpty(userName),"用户名不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(password),"密码不能为空!");
        User user = this.findUserByUserName(userName);
        AssertUtil.isTrue(null == user,"用户不存在或已注销");
        AssertUtil.isTrue(!(user.getPassword().equals(password)),"密码不正确,请重新输入");
        // TODO 后期通过SpringSecurity实现完全的权限验证接管

        return user;
    }

    @Override
    public User findUserByUserName(String userName) {

        User user = userMapper.selectOne(new QueryWrapper<User>().eq("is_del", 0).eq("user_name", userName));
        return user;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateUserInfo(User user) {
        /**
         * 更新时保证用户名非空且唯一
         */
        AssertUtil.isTrue(StringUtil.isEmpty(user.getUserName()),"用户名不能为空!");
        User tempUser = this.findUserByUserName(user.getUserName());
        // 如果根据用户更新的id,能找到对应的已存在用户
        // 且找到的用户不是当前用户本身,则用户id是已被占用的
        AssertUtil.isTrue(null != tempUser && !(tempUser.getId().equals(user.getId())),"用户名已存在!");

        AssertUtil.isTrue((!this.updateById(user)),"更新用户信息失败!");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updatePassword(String oldPassword, String newPassword, String confirmPassword, String userName) {
        /**
         * 确保输入的旧密码和数据库中该用户的旧密码一致
         * 且两次输入的新密码一致
         * 旧密码不能与新密码相同
         */
        User tempUser = null;
        tempUser = this.findUserByUserName(userName);
        AssertUtil.isTrue(StringUtil.isEmpty(oldPassword),"旧的密码不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(newPassword),"新的密码不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(confirmPassword),"确认密码不能为空!");

        AssertUtil.isTrue((!newPassword.equals(confirmPassword)),"两次输入新密码不一致");
        AssertUtil.isTrue((oldPassword.equals(newPassword)),"新密码不能与原密码相同");
        AssertUtil.isTrue(!(oldPassword.equals(tempUser.getPassword())),"原密码输入错误!");

        tempUser.setPassword(newPassword);
        AssertUtil.isTrue((!this.updateById(tempUser)),"更新用户密码失败!");
    }
}
